package com.loongtech.core.jpa.entity;

import java.io.Serializable;

import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

@MappedSuperclass
public abstract class EntityBase implements Serializable {
	private static final long serialVersionUID = 1L;
	public static final int VCHAR_MIDDLE = 1024;
	public static final int VCHAR_NORMAL = 2048;
	public static final int VCHAR_LARGE = 10240;

	public static final int BLOB_MIDDLE = 512;
	public static final int BLOB_LARGE = 5120;

	@Version
	private int version;			//乐观锁
	
	//ID关键字
	public abstract Serializable id();

	
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
	
	@Override
	public boolean equals(Object other) {
		if(this == other) {
			return true;
		}
		if(!(other instanceof EntityBase)) {
			return false;
		}
		EntityBase castOther = (EntityBase) other;
		return new EqualsBuilder().append(this.id(), castOther.id()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(id()).toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id()).toString();
	}
}